package managerEngine

import (
	"gitee.com/RocsSun/report/config"
	"github.com/go-resty/resty/v2"
)

type NetFlowRESTClient struct {
	Token string
}

func NewNetFlowRESTClient(token string) *NetFlowRESTClient {
	return &NetFlowRESTClient{
		Token: token,
	}
}

func NewDefaultNetFlowRESTClient() *NetFlowRESTClient {
	return &NetFlowRESTClient{
		Token: config.ConfigContext.ManagerEngine.NetFlowAnalyzer.Token,
	}
}

func (n *NetFlowRESTClient) GetNetWorkData(url string, params map[string]string) (*resty.Response, error) {
	params["apiKey"] = n.Token
	return Spider.R().SetQueryParams(params).Get(url)
}

func (n *NetFlowRESTClient) ListDevices() *resty.Response {
	urls := config.ConfigContext.ManagerEngine.NetFlowAnalyzer.URI.BaseURL + config.ConfigContext.ManagerEngine.NetFlowAnalyzer.API.ListDevices
	params := map[string]string{}
	resp, err := n.GetNetWorkData(urls, params)
	if err != nil {
		return resp
	}
	return resp
}

func (n *NetFlowRESTClient) DailyIndexInterfaceData(deviceId string) *resty.Response {
	urls := config.ConfigContext.ManagerEngine.NetFlowAnalyzer.URI.BaseURL + config.ConfigContext.ManagerEngine.NetFlowAnalyzer.API.IndexInterfaceData
	params := map[string]string{
		"TimeFrame": "Daily",
		"DeviceID":  deviceId,
	}
	resp, err := n.GetNetWorkData(urls, params)
	if err != nil {
		return resp
	}
	return resp
}

func (n *NetFlowRESTClient) ConvData(deviceId string) *resty.Response {
	var params map[string]string = make(map[string]string)
	urls := config.ConfigContext.ManagerEngine.NetFlowAnalyzer.URI.BaseURL + config.ConfigContext.ManagerEngine.NetFlowAnalyzer.API.ConvData
	params["TimeFrame"] = "Daily"
	params["DeviceID"] = deviceId
	params["tablegridviewtype"] = "Chart"
	params["Data"] = "IN"
	params["pageCount"] = "1"
	params["rows"] = "50"
	params["isNetwork"] = "false"

	resp, _ := n.GetNetWorkData(urls, params)
	return resp
}
